
#pragma once
#include<iostream>
#include<assert.h>
#include<string.h>

namespace wjb
{
	class string
	{
	public:
		typedef char* iterator;



		string(const char* str = "")
			:_size(strlen(str)),
			_capacity(_size)
		{
			if (_size == 0)
			{
				_str = new char[1];
			}

			else
				_str = new char[_capacity + 1];
			for (size_t i = 0; i <= _size; i++)
			{
				_str[i] = str[i];
			}

		}


		~string()
		{
			delete[] _str;
			_str = nullptr;
			_size = 0;
			_capacity = 0;
		}

		const char* c_str()const
		{
			return _str;
		}

		char& operator[](size_t pos)
		{
			return _str[pos];
		}
		const char& operator[](size_t pos)const
		{
			return _str[pos];
		}

		size_t size()
		{
			return _size;
		}

		char* begin()
		{
			return _str;
		}
		char* end()
		{
			return _str + _size;
		}
		const char* begin()const
		{
			return _str;
		}
		const char* end()const
		{
			return _str + _size;
		}

		void pushback(char ch)
		{
			if (_size == 0)
			{
				reserve(1);
			}
			if (_size == _capacity)
			{
				reserve(_capacity * 2);
			}
			_str[_size++] = ch;
			_str[_size] = '\0';
		}
		void append(const char* str)
		{
			size_t len = strlen(str);
			if (_size + len > _capacity)
			{
				reserve(_size + len);
				for (size_t i = 0; i <= len; i++)
				{
					_str[_size + i] = str[i];
				}
			}
			_size += len;

		}


		string& operator+=(char ch)
		{
			pushback(ch);
			return *this;
		}

		string& operator+=(const char* str)
		{
			append(str);
			return *this;
		}


		void reserve(size_t n)
		{
			if (n > _capacity)
			{
				char* tmp = new char[n + 1];
				memcpy(tmp, _str,_size+1);
				delete[] _str;
				_str = tmp;
				tmp = nullptr;
				_capacity = n;
			}
		}

		void insert(size_t pos, size_t n, char ch)
		{
			assert(pos < _size);
			if (_size + n > _capacity)
			{
				reserve(_size + n);
			}
			size_t end = _size;
			while (end >= pos && end != npos)
			{
				_str[end + n] = _str[end];
				end--;
			}
			for (size_t i = 0; i < n; i++)
			{
				_str[pos + i] = ch;
			}
			_size += n;
		}
		void insert(size_t pos, const char* str)
		{
			assert(pos < _size);
			size_t len = strlen(str);
			if (_size + len > _capacity)
			{
				reserve(_size + len);
			}
			size_t end = _size;
			while (end >= pos && end != npos)
			{
				_str[end + len] = _str[end];
				--end;
			}
			for (size_t i = 0; i < len; i++)
			{
				_str[pos + i] = str[i];
			}
			_size += len;
		}

		void erase(size_t pos = 0, size_t len = npos)
		{
			assert(pos < _size);
			if (pos + len >= _size || len == npos)
			{
				_str[pos] = '\0';
			}
			else
			{
				size_t end = _size;
				while (end >= pos && pos + len <= _size)
				{
					_str[pos] = _str[pos + len];
					--end;
					++pos;
				}
			}
			_size -= len;
		}

		size_t find(char ch, size_t pos = 0)const
		{
			assert(pos < _size);
			for (size_t i = 0; i + pos < _size; i++)
			{
				if (_str[i + pos] = ch)
				{
					return i;
				}
			}
			return npos;
		}

		size_t find(const char* str, size_t pos = 0)const
		{
			assert(pos < _size);
			const char* tmp = strstr(_str + pos, str);
			if (tmp == nullptr)
			{
				return npos;
			}
			return tmp - _str;
		}

		string(const string& s)
		{
			_str = new char[s._capacity + 1];
			_size = s._size;
			_capacity = s._capacity;
			for (size_t i = 0; i <= _size; i++)
			{
				_str[i] = s._str[i];
			}
		}

		string substr(size_t pos = 0, size_t len = npos)const
		{
			assert(pos < _size);
			string tmp;

			for (size_t i = 0; i < len && i + pos < _size; i++)
			{
				tmp += _str[pos + i];
			}
			return tmp;
		}

		void resize(size_t n, char ch = '\0')
		{
			if (n < _size)
			{
				_str[n] = '\0';
			}
			else
			{
				reserve(n);
				for (size_t i = _size; i < n; i++)
				{
					_str[i] = ch;
				}
				_str[n] = '\0';
			}
			_size = n;
		}

		void clear()
		{
			_str[0] = '\0';
			_size = 0;
		}
		
		bool operator<(const string& s)const
		{
			int ret = memcmp(this, &s,_size<s._size ? _size : s._size);
			return ret == 0 ? _size < s._size : ret < 0;
		}

		bool operator==(const string& s)const
		{
			int ret = memcmp(this, &s, _size < s._size ? _size : s._size);
			return ret == 0 ? _size == s._size : false;
		}

		bool operator<=(const string& s)const
		{
			return *this < s || *this == s;
		}

		bool operator>(const string& s)const
		{
			return !(*this <= s);
		}

		bool operator>=(const string& s)const
		{
			return !(*this < s);
		}

		bool operator!=(const string& s)const
		{
			return !(*this == s);
		}

		void swap(string& s)
		{
			std::swap(_str, s._str);
			std::swap(_size, s._size);
			std::swap(_capacity, s._capacity);
		}

		string& operator=(string tmp)
		{
			swap(tmp);
			return *this;
		}
		
		//现代拷贝，不能拷贝"hello\0xxxx"这种对象
		
		string(string& s)
		_str = nullptr;
		_size = 0;
		_capacity = 0;
		{
			string tmp(s._str);
			swap(*this,tmp);
		}


	private:
		size_t _size;
		size_t _capacity;
		char* _str;
	public:
		static size_t npos;
	};
	size_t string::npos = -1;
	std::ostream& operator<<(std::ostream& out, string& s)
	{
		for (size_t i = 0; i < s.size(); i++)
		{
			out << s[i];
		}
		return out;
	}
	std::istream& operator>>(std::istream& in, string& s)
	{
		s.clear();
		char buff[128];
		char ch = in.get();
		while (ch == ' ' || ch == '\n')
		{
			ch = in.get();
		}
		for (size_t i = 0; i < 128; ++i)
		{
			buff[i] = ch;
			if (i == 127)
			{
				buff[i] = '\0';
				s += buff;
				i = 0;
			}
			ch = in.get();
			if (ch == ' ' || ch == '\n')
			{
				buff[i + 1] = '\0';
				break;
			}
		}

		s += buff;
		return in;
	}
}



